home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Functions as parameters
- Date: Fri, 23 Feb 1996 07:09:26 GMT
- Organization: Netcom
- Message-ID: <312d6726.112558110@nntp.ix.netcom.com>
- References: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
- NNTP-Posting-Host: ix-dc6-15.ix.netcom.com
- X-NETCOM-Date: Thu Feb 22 11:09:21 PM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves) wrote:
-
- >
- > I've often seen and even written code that uses function pointers like
- > this:
- >
- >
- > /* cut here */
- > #include <stdio.h>
- >
- > void foo( int (*func)( int ) ){
- > printf("%d\n", func( 5 ) );
- > }
- >
- > int bar( int x ){
- > return x*2;
- > }
- >
- > int
- > main(){
- > foo( bar );
- > return 0;
- > }
- > /* cut here */
- >
- > Recently though, I saw a similar piece of code that did something like
- > this:
- >
- > /* cut here */
- > void foo( int func( int ) ){
- > printf("%d\n", func( 5 ) );
- > }
- > /* cut here */
- >
- > I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
- > worked. So my question is: is this standard, or is this some weird compiler
- > extension. I've never seen this syntax before. Does it mean the same thing
- > as the first piece of code? The body of the two functions is identical, and
- > they're called in the same way.
- >
- > If they are identical, why is the (*func)(int) syntax so much more common?
- > The func(int) syntax does seem easier to type as well as read, but I've
- > never seen it before.
-
- Either is legal. I'm not sure why the pointer notation is more
- common; probably mainly habit.
-
- Note that the type of the parameter is pointer to function regardless
- of how you write it. The compiler silently treates a function
- declaration in a parameter list to a pointer to function declaraton
- similar to the way it converts an array declaration to a pointer
- declaration.
-
- Michael M Rubenstein
-